package com.api.workflow.web;

import com.api.workflow.util.YNHttpUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/*
 *  OA-06  资产主数据创建接口
 *
 * */
public class YNFII011Action extends BaseBean implements Action {
    public static void main(String[] args) {

    }
    @Override
    public String execute(RequestInfo requestInfo) {

        BaseBean baseBean = new BaseBean();
        String workflowid = requestInfo.getWorkflowid();
        String requestid = requestInfo.getRequestid();
        int formid = requestInfo.getRequestManager().getFormid();
        int formidAbs = Math.abs(formid);// 流程表单id绝对值
        String tableName ="formtable_main_"+formidAbs;


        // 主表数据
        MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
        Property[] property = mainTableInfo.getProperty();


        //明细表数据
        DetailTable[] detailTable = requestInfo.getDetailTableInfo().getDetailTable();
        RecordSet recordSet1 = new RecordSet();
        RecordSet recordSet2 = new RecordSet();
        recordSet1.executeQuery("select * from "+tableName+" where requestid=?",requestid);
        recordSet1.next();
        String id = Util.null2String(recordSet1.getString("id"));

        String table = property.toString();

        String detail = detailTable.toString();


        baseBean.writeLog("wyq==资产主数据创建==========table" + com.alibaba.fastjson.JSONObject.toJSONString(table));
        baseBean.writeLog("wyq==资产主数据创建==========detail" + com.alibaba.fastjson.JSONObject.toJSONString(detail));


        String message = "";
        try {

            RecordSet rs = new RecordSet();


            Map<String, Object> map = new HashMap<>();

            for (Property property1 : property) {
                String name = property1.getName().toLowerCase();
                baseBean.writeLog("wyq==资产主数据创建=========property1" + name + "wyq" + property1.getValue());
                if (name.equals("sqr")) {

                    String sqbm = "";
                    String sqr = property1.getValue();
                    String sql1 = "select * from hrmresource where id = '" + sqr + "'";
                    baseBean.writeLog("wyq==资产主数据创建===========sql1" + sql1);
                    rs.execute(sql1);
                    if (rs.next()) {
                        sqbm = rs.getString("departmentid");
                        map.put("ZUSER", rs.getString("lastname"));
                        baseBean.writeLog("wyq==资产主数据创建=========lastname" + rs.getString("lastname"));
                    }

                    String sql2 = "select * from hrmdepartment where id = '" + sqbm + "'";
                    baseBean.writeLog("wyq==资产主数据创建===========sql2" + sql2);

                    rs.execute(sql2);
                    if (rs.next()) {
                        map.put("ZSQBM", rs.getString("departmentcode"));
                        map.put("ZBMMS", rs.getString("departmentname"));
                        baseBean.writeLog("wyq==资产主数据创建=========ZSQBM" + rs.getString("departmentcode") + "ZBMMS:" + rs.getString("departmentname"));
                    }
                }
                if (name.equals("lcbh")) {
                    map.put("ZIFSN", property1.getValue());
                }
                //map.put("ZIFSN", requestInfo.getRequestid());
                if (name.equals("gsdm")) {
                    map.put("BUKRS", property1.getValue());
                }

            }


            String tableDBName = "";
            ArrayList<Map> list = new ArrayList<>();
            int count = 1;
            recordSet1.executeQuery("select * from "+tableName+"_dt1 where mainid=?",id);
            while (recordSet1.next()){
                Map<String, Object> ITEM = new HashMap<>();
                ITEM.put("ZITEM", count);
                String mxid = Util.null2String(recordSet1.getString("id"));
                writeLog("执行修改开始！");
                recordSet2.executeUpdate("update "+tableName+"_dt1 set mxxh=? where id=?",String.valueOf(count),mxid);
                writeLog("执行修改成功！");
                ITEM.put("ANLKL", Util.null2String(recordSet1.getString("zcfl")));
                ITEM.put("TXT50", Util.null2String(recordSet1.getString("zcmc")));
                ITEM.put("MEINS", Util.null2String(recordSet1.getString("zcdw")));
                ITEM.put("MENGE", Util.null2String(recordSet1.getString("sgsl")));
                ITEM.put("BUKRS", Util.null2String(recordSet1.getString("gsdm")));
                ITEM.put("KOSTL", Util.null2String(recordSet1.getString("cbzx")));
                ITEM.put("KOSTLV", Util.null2String(recordSet1.getString("cbzx")));
                ITEM.put("ZJHJE", Util.null2String(recordSet1.getString("zcygje")));
                ITEM.put("ORD41", Util.null2String(recordSet1.getString("zcly")));
                ITEM.put("ORD43", Util.null2String(recordSet1.getString("zcyt")));
                ITEM.put("SERNR", Util.null2String(recordSet1.getString("ggxh")));
                count++;
                list.add(ITEM);
            }


            map.put("ITEMS", list);


            JSONObject object = new JSONObject(map);
            baseBean.writeLog("wyq==资产主数据创建=========object" + object.toString());

            String post = YNHttpUtils.FII011(object.toString(), YNHttpUtils.token());

            baseBean.writeLog("wyq==资产主数据创建=========post" + post);
            JSONObject jsonObject = JSONObject.parseObject(post);


            String STATUS = jsonObject.getString("STATUS");
            String MESSAGE = jsonObject.getString("MESSAGE");

            JSONArray data = jsonObject.getJSONArray("RETURN");
            for (int i = 0; i < data.size(); i++) {

                JSONObject jsonObject1 = data.getJSONObject(i);
                String TYPE = MESSAGE + jsonObject1.getString("TYPE");
                message = MESSAGE + jsonObject1.getString("MESSAGE");
                
                RecordSet recordSet = new RecordSet();
                //修改主表参数
                String updateMainTableName = "update " + tableName + " set qqcsx = ? , fhjgx = ?  where  requestid = ? ";
                recordSet.executeUpdate(updateMainTableName, object.toString(), post, requestid);
                baseBean.writeLog("wyq==资产主数据创建===========updateMainTableName" + updateMainTableName);

                if (!"S".equals(TYPE)) {
                    requestInfo.getRequestManager()
                            .setMessageid(requestInfo.getRequestid() + "OA-资产主数据创建接口");
                    requestInfo.getRequestManager().setMessagecontent("SAP接口异常，请联系SAP人员，错误信息提示:" + MESSAGE+"//"+message);
                    return Action.FAILURE_AND_CONTINUE;
                }


                String ZITEM = jsonObject1.getString("ZITEM");
                String ANLN1 = jsonObject1.getString("ANLN1");

                RecordSet recordSet3 = new RecordSet();
                String sql="update "+tableName+"_dt1 set zckph ='"+ANLN1+"' where mainid="+id+" and mxxh='"+ZITEM+"'";
                writeLog("sqlsqlsqlsqlsqlsql"+sql);
                recordSet3.executeUpdate(sql);


            }

            baseBean.writeLog("wyq==资产主数据创建===========资产主数据创建接口");



        } catch (Exception e) {
            baseBean.writeLog("wyq==资产主数据创建======exc" + e.toString());
            requestInfo.getRequestManager()
                    .setMessageid(requestInfo.getRequestid() + "OA-资产主数据创建接口");
            requestInfo.getRequestManager().setMessagecontent("OA接口异常：" + e.toString());
            return Action.FAILURE_AND_CONTINUE;
        }

        return Action.SUCCESS;
    }
}
